﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YiSha.Admin.WebApi.Model;
using YiSha.Business.YiDongYuan;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.WebApi.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class WeatherController : ControllerBase
    {
        #region 获取数据

        /// <summary>
        /// 根据经纬度获取天气信息
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<grid_now>> GetWeatherInfo([FromQuery] string lng = "111.01701", [FromQuery] string lat = "35.05006")
        {
            TData<grid_now> obj = new TData<grid_now>();
            obj.Tag = 0;
            string url = string.Format(@"https://api.heweather.net/s6/weather/grid-now?location={0},{1}&key={2}&v=1"
           , lng, lat, "85b99b90dc0647568dfc5df659072c6b");
            string result = HttpHelper.HttpGet(url);
            HeWeather weather = result.ToObject<HeWeather>();
            if (weather.HeWeather6 != null && weather.HeWeather6.Count > 0)
            {
                if (weather.HeWeather6[0].status != "ok")
                {
                    obj.Message = "获取气象数据出错";
                }
                else
                {
                    obj.Message = result;
                    obj.Data = weather.HeWeather6[0].grid_now;
                }
            }
            else
            {
                obj.Message = "没有获取到气象数据";
            }
            return obj;
        }

        #endregion 获取数据
    }
}